/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialgroup.service;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import cn.knet.sp.ncrm.common.service.CrudService;
import cn.knet.sp.ncrm.common.service.ServiceException;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.dao.PotentialCustomerDao;
import cn.knet.sp.ncrm.modules.customer.potentialgroup.dao.PotentialGroupDao;
import cn.knet.sp.ncrm.modules.customer.potentialgroup.entity.PotentialGroup;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 分组Service
 *
 * @author 冯亚鹏
 * @version 2015-08-24
 */
@Service
@Transactional(readOnly = true)
public class PotentialGroupService extends CrudService<PotentialGroupDao, PotentialGroup> {


	@Resource
	private PotentialCustomerDao potentialCustomerDao;

	public PotentialGroup get(String id) {
		return super.get(id);
	}

	public List<PotentialGroup> findList(PotentialGroup potentialGroup, Boolean... addDefault) {
		if (null != potentialGroup && StringUtils.isNotBlank(potentialGroup.getParentIds())) {
			potentialGroup.setParentIds("," + potentialGroup.getParentIds() + ",");
		}
		List<PotentialGroup> groups = super.findList(potentialGroup);
		//默认分组
		if (addDefault.length > 0 && BooleanUtils.isTrue(addDefault[0])) {
			PotentialGroup defaultAdminGroup = new PotentialGroup();
			defaultAdminGroup.setId("default");
			defaultAdminGroup.setParent(null);
			defaultAdminGroup.setName("未分组");
			groups.add(0, defaultAdminGroup);
			return groups;
		} else {
			return groups;
		}
	}

	@Transactional(readOnly = false)
	public void save(PotentialGroup potentialGroup) {
		//判断父级是否【未分组】
		if (potentialGroup.getParentId().equals("default")) {
			throw new ServiceException("【未分组】不能添加下级分组");
		}
		//判断是否存在同名分组
		PotentialGroup tempGroup = this.getByName(potentialGroup.getName());
		if (tempGroup != null) {
			if (!tempGroup.getId().equals(potentialGroup.getId())) {
				throw new ServiceException("存在同名的分组，添加失败");
			}
		}
		//如何parentId等于0，则为顶级分组，level设置为1
		if (org.apache.commons.lang3.StringUtils.isBlank(potentialGroup.getParentId()) || org.apache.commons.lang3.StringUtils
				.equals(potentialGroup.getParentId(), "0")) {
			PotentialGroup parentGroup= new PotentialGroup("0");
			potentialGroup.setParent(parentGroup);
			potentialGroup.setLevelNum(1);
		} else {
			PotentialGroup parentGroup = get(potentialGroup.getParentId());
			if (parentGroup != null && StringUtils.isNotBlank(parentGroup.getId())) {
				Integer levelNum = parentGroup.getLevelNum();
				//管理员分组限制3级
				if (levelNum == 2) {
					throw new ServiceException("最多只能添加2级分组，添加失败");
				}
				potentialGroup.setLevelNum(parentGroup.getLevelNum() + 1);
			} else {
				throw new ServiceException("选择的上级分组已经被删除");
			}
		}
		super.save(potentialGroup);
	}

	@Transactional(readOnly = false)
	public void delete(PotentialGroup potentialGroup) {
		HashMap paramsMap = new HashMap();
		paramsMap.put("group1", null);
		paramsMap.put("group2", null);
		paramsMap.put("realGroup", null);
		paramsMap.put("hasGroup", "no");
		paramsMap.put("lastUpdated",new Date());
		paramsMap.put("updateGroupId",potentialGroup.getId());
		potentialCustomerDao.updateGroupInfo(paramsMap);
		super.delete(potentialGroup);
	}

	/**
	 *  通过分组名称获取分组信息
	 * @author Gehaiyang
	 * @since 2015 /09/18 10:00:15
	 * @param groupName
	 * @date 2015年9月15日 上午9:54:59
	 * @return
	 */
	public PotentialGroup getByName(String groupName) {
		PotentialGroup potentialGroup = new PotentialGroup();
		potentialGroup.setUser(UserUtils.getUser());
		potentialGroup.setName(groupName);
		return this.dao.getByName(potentialGroup);
	}
}